package com.yuntuzhilian.chapter5.demo3;

import java.io.*;
import java.net.Socket;

/**
 * @author MrWang-shuizhishuiheshui
 * @description 客户端--请求服务器
 */
public class ClientTest {
    public static void main(String[] args) {
        Socket socket=null;
        OutputStream outputStream =null;
        InputStream inputStream =null;
        BufferedReader br=null;
        ObjectOutputStream objectOutputStream=null;
        try {
            //1.创建socket对象,指明访问的ip地址和端口号
            socket=new Socket("localhost",8000);
            //2.打开输出流,发送请求
            outputStream = socket.getOutputStream();
            //  创建输入流,接收
            inputStream = socket.getInputStream();

            //3.发送信息
            Student stu=new Student("aaaaaa","321312");
            //将输出流转化为对象输出流
            objectOutputStream=new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(stu);
            //关闭输出
            socket.shutdownOutput();

            //4.接收服务器返回数据
            String str=null;
            br=new BufferedReader(new InputStreamReader(inputStream));
            while((str=br.readLine())!=null){
                System.out.println("我是客户端,我接受服务器返回的数据为:"+str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                objectOutputStream.close();
                outputStream.close();
                br.close();
                inputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
